package com.learn.smart.framework.proxy;

import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AspectProxy implements Proxy {
	
	private static final Logger log = LoggerFactory.getLogger(AspectProxy.class);

	@Override
	public Object doProxy(ProxyChain proxyChain) throws Throwable  {
		Object result = null;
		Class<?> cls = proxyChain.getTargetClass();
		Method method = proxyChain.getTargetMethod();
		Object[] params = proxyChain.getMethodParams();
		
		begin();
		try{
			if(intercept(cls,method,params)){
				before(cls,method,params);
				result = proxyChain.doProxyChain();
				after(cls,method,params,result);
			}else{
				result = proxyChain.doProxyChain();
			}
		}catch(Exception e){
			log.error("代理失败",e);
			error(cls,method,params,e);
			throw e;
		}finally{
			end();
		}
		
		
		return null;
	}

	public void end() {
		// TODO Auto-generated method stub
		
	}

	public void error(Class<?> cls, Method method, Object[] params, Exception e) {
		// TODO Auto-generated method stub
		
	}

	public void after(Class<?> cls, Method method, Object[] params,Object result) {
		// TODO Auto-generated method stub
		
	}

	public void before(Class<?> cls, Method method, Object[] params) {
		// TODO Auto-generated method stub
		
	}

	public boolean intercept(Class<?> cls, Method method, Object[] params) {
		// TODO Auto-generated method stub
		return false;
	}

	public void begin() {
		// TODO Auto-generated method stub
		
	}

}
